package com.aps.server.controller;

import com.aps.algorithm.entity.ApsKpiTemplate;
import com.aps.common.entity.ApsCode;
import com.aps.common.entity.ExtMapUtils;
import com.aps.common.entity.Result;
import com.aps.common.service.ApsCodeService;
import com.aps.server.service.ApsSystemConfigrationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author nbZhuozh
 * @Date 2025-11-21  09:56
 **/
@RestController
@Tag(name = "配置中心", description = "系统参数配置api")
public class ApsSystemConfigrationController {

    @Resource
    ApsSystemConfigrationService apsSystemConfigrationService;

    @Resource
    ApsCodeService apsCodeService;


    //查询当前租户下的 KPI模板
    @GetMapping("queryKpiTemplateNames")
    public Result<Map<String, Object>> getSystemConfig() {
        //当前kpi数据 对应的 行业模板名称  + 模板code 和 描述
        Map<String, Object> resultMap = new HashMap<>();
        //获取用户对应的模板数据
        List<ApsKpiTemplate> apsKpiTemplates = apsSystemConfigrationService.queryKpiTemplateConfig();
        if(CollectionUtils.isEmpty(apsKpiTemplates)){
            return Result.success(Collections.emptyMap());
        }
        //根据模板数据获取所有的行业编码
        List<String> result = apsKpiTemplates.stream().map(ApsKpiTemplate::getIndustryCode).distinct().toList();
        for (String codeValue : result) {
            //查询code表
            ApsCode apsCodes = apsCodeService.selectByCodeValue(codeValue, "SYS_INDUSTRY_TYPE");
            if(apsCodes != null){
                apsCodes.toExt$Map(null,apsCodes.getExtJson());
                resultMap.put(codeValue,apsCodes);
            }
        }
        return Result.success(resultMap);
    }

    //查询租户 KPI分析配置
//    public List<ApsKpiTemplate> queryKpiTemplateConfig(){
//
//    }

    //查询算法参数配置

    //查询 系统参数配置
}
